package com.industrial.operation.service.impl;

import java.util.Date;
import java.util.List;

import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.industrial.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.industrial.operation.domain.OperationKnowledgeTexts;
import com.industrial.operation.mapper.OperationKnowledgeManageMapper;
import com.industrial.operation.domain.OperationKnowledgeManage;
import com.industrial.operation.service.IOperationKnowledgeManageService;

/**
 * 知识动态管理Service业务层处理
 * 
 * @author ZHOU_S
 * @date 2024-02-20
 */
@Service
public class OperationKnowledgeManageServiceImpl implements IOperationKnowledgeManageService 
{
    @Autowired
    private OperationKnowledgeManageMapper operationKnowledgeManageMapper;

    /**
     * 查询知识动态管理
     * 
     * @param baseId 知识动态管理主键
     * @return 知识动态管理
     */
    @Override
    public OperationKnowledgeManage selectOperationKnowledgeManageByBaseId(String baseId)
    {
        return operationKnowledgeManageMapper.selectOperationKnowledgeManageByBaseId(baseId);
    }

    /**
     * 查询知识动态管理列表
     * 
     * @param operationKnowledgeManage 知识动态管理
     * @return 知识动态管理
     */
    @Override
    public List<OperationKnowledgeManage> selectOperationKnowledgeManageList(OperationKnowledgeManage operationKnowledgeManage)
    {
        return operationKnowledgeManageMapper.selectOperationKnowledgeManageList(operationKnowledgeManage);
    }

    /**
     * 新增知识动态管理
     * 
     * @param operationKnowledgeManage 知识动态管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOperationKnowledgeManage(OperationKnowledgeManage operationKnowledgeManage)
    {
        operationKnowledgeManage.setBaseId(IdUtils.simpleUUID());
        operationKnowledgeManage.setBaseCreateTime(new Date());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        operationKnowledgeManage.setCreator(loginUser.getUserid());
        int rows = operationKnowledgeManageMapper.insertOperationKnowledgeManage(operationKnowledgeManage);
        insertOperationKnowledgeTexts(operationKnowledgeManage);
        return rows;
    }

    /**
     * 修改知识动态管理
     * 
     * @param operationKnowledgeManage 知识动态管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOperationKnowledgeManage(OperationKnowledgeManage operationKnowledgeManage)
    {

      /*  operationKnowledgeManageMapper.deleteOperationKnowledgeTextsByKnowledgeId(operationKnowledgeManage.getBaseId());
        insertOperationKnowledgeTexts(operationKnowledgeManage);*/
        return operationKnowledgeManageMapper.updateOperationKnowledgeManage(operationKnowledgeManage);
    }

    /**
     * 批量删除知识动态管理
     * 
     * @param baseIds 需要删除的知识动态管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOperationKnowledgeManageByBaseIds(String[] baseIds)
    {
        operationKnowledgeManageMapper.deleteOperationKnowledgeTextsByKnowledgeIds(baseIds);
        return operationKnowledgeManageMapper.deleteOperationKnowledgeManageByBaseIds(baseIds);
    }

    /**
     * 删除知识动态管理信息
     * 
     * @param baseId 知识动态管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOperationKnowledgeManageByBaseId(String baseId)
    {
        operationKnowledgeManageMapper.deleteOperationKnowledgeTextsByKnowledgeId(baseId);
        return operationKnowledgeManageMapper.deleteOperationKnowledgeManageByBaseId(baseId);
    }

    /**
     * 新增知识动态管理正文信息
     * 
     * @param operationKnowledgeManage 知识动态管理对象
     */
    public void insertOperationKnowledgeTexts(OperationKnowledgeManage operationKnowledgeManage)
    {
        List<OperationKnowledgeTexts> operationKnowledgeTextsList = operationKnowledgeManage.getOperationKnowledgeTextsList();
        String baseId = operationKnowledgeManage.getBaseId();
        if (StringUtils.isNotNull(operationKnowledgeTextsList))
        {
            List<OperationKnowledgeTexts> list = new ArrayList<OperationKnowledgeTexts>();
            for (OperationKnowledgeTexts operationKnowledgeTexts : operationKnowledgeTextsList)
            {
                operationKnowledgeTexts.setBaseId(IdUtils.simpleUUID());
                operationKnowledgeTexts.setKnowledgeId(baseId);
                list.add(operationKnowledgeTexts);
            }
            if (list.size() > 0)
            {
                operationKnowledgeManageMapper.batchOperationKnowledgeTexts(list);
            }
        }
    }
}
